<template>
  <div class="app-container">

    <el-table :data="list" border fit highlight-current-row style="width: 100%" v-loading="listLoading">

      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Date" width="180px">
        <template slot-scope="scope">
          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Author" width="120px">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>

      <el-table-column label="Importance" width="100px">
        <template slot-scope="scope">
          <svg-icon :key="n" class="meta-item__icon" icon-class="star" v-for="n in +scope.row.importance"/>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="Status" width="110">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="Title" min-width="300px">
        <template slot-scope="scope">
          <template v-if="scope.row.edit">
            <el-input class="edit-input" size="small" v-model="scope.row.title"/>
            <el-button @click="cancelEdit(scope.row)" class="cancel-btn" icon="el-icon-refresh" size="small"
                       type="warning">cancel
            </el-button>
          </template>
          <span v-else>{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="120">
        <template slot-scope="scope">
          <el-button @click="confirmEdit(scope.row)" icon="el-icon-circle-check-outline" size="small" type="success"
                     v-if="scope.row.edit">Ok
          </el-button>
          <el-button @click="scope.row.edit=!scope.row.edit" icon="el-icon-edit" size="small" type="primary" v-else>
            Edit
          </el-button>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
  import {fetchList} from '@/api/article'

  export default {
    name: 'InlineEditTable',
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger'
        };
        return statusMap[status]
      }
    },
    data() {
      return {
        list: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          const items = response.data.items;
          this.list = items.map(v => {
            this.$set(v, 'edit', false); // https://vuejs.org/v2/guide/reactivity.html
            v.originalTitle = v.title; //  will be used when user click the cancel botton
            return v
          });
          this.listLoading = false
        })
      },
      cancelEdit(row) {
        row.title = row.originalTitle;
        row.edit = false;
        this.$message({
          message: 'The title has been restored to the original value',
          type: 'warning'
        })
      },
      confirmEdit(row) {
        row.edit = false;
        row.originalTitle = row.title;
        this.$message({
          message: 'The title has been edited',
          type: 'success'
        })
      }
    }
  }
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }

  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
